//
//  WJLoadingAnimation.m
//  活动指示器
//
//  Created by 谭启宏 on 16/4/19.
//  Copyright © 2016年 tqh. All rights reserved.
//

#import "WJLoadingAnimation.h"

@interface WJLoadingAnimation ()



@property (nonatomic,strong) CAReplicatorLayer *replicator;
@property (nonatomic,strong) CALayer *indicator;
@property (nonatomic,strong) CABasicAnimation *anim;
/** 判断当前动画属性 */
@property (nonatomic,assign) BOOL isAnimating;

@end

@implementation WJLoadingAnimation


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self common];
        [self.layer addSublayer:self.replicator];
        //添加层
        [self.replicator addSublayer:self.indicator];
    }
    return self;
}

- (void)common {
    self.count = 15;
    self.durationTime  = 1.0;
}

- (void)layoutSubviews {
    self.replicator.frame = self.bounds;
}

//懒加载，复制层
- (CAReplicatorLayer *)replicator
{
    if (_replicator == nil) {
        _replicator = [CAReplicatorLayer layer];
        _replicator.frame = self.bounds;
        // 设置子层次数
        _replicator.instanceCount = self.count;
        // 设置子层动画延长时间
        _replicator.instanceDelay = self.durationTime / self.count;
        // 设置子层形变角度
        CGFloat angle = M_PI * 2 / self.count;
        _replicator.instanceTransform = CATransform3DMakeRotation(angle, 0, 0, 1);
    }
    return _replicator;
}

//普通层
- (CALayer *)indicator
{
    if (_indicator == nil) {
        _indicator = [CALayer layer];
        _indicator.backgroundColor = [UIColor colorWithRed:0.463 green:0.337 blue:0.439 alpha:1.000].CGColor;
        _indicator.cornerRadius = 7.5;
        _indicator.masksToBounds = YES;
        _indicator.transform = CATransform3DMakeScale(0.01, 0.01, 0.01);
        _indicator.bounds = CGRectMake(0, 0, 15, 15);
    }
    return _indicator;
}

//动画
- (CABasicAnimation *)anim
{
    if (_anim == nil) {
        _anim = [CABasicAnimation animation];
        _anim.keyPath = @"transform.scale";
        _anim.fromValue = @1;
        _anim.toValue = @0.1;
        _anim.repeatCount = MAXFLOAT;
        _anim.duration = self.durationTime;
    }
    return _anim;
}

/** 开始动画 */
- (void)startAnimating {
    //添加动画
    if (self.isAnimating) {
        return;
    }else {
        [self.indicator addAnimation:self.anim forKey:@"indicatorAnimation"];
        self.isAnimating = YES;
    }
    
}

/** 停止动画 */
- (void)stopAnimating {
    [self.indicator removeAnimationForKey:@"indicatorAnimation"];
    self.isAnimating = NO;
}

#pragma mark - set

- (void)setColor:(UIColor *)color {
    _color = color;
     self.indicator.backgroundColor = _color.CGColor;
}

- (void)setCount:(NSInteger)count {
    _count = count;
    [self change];
}

- (void)setDurationTime:(CGFloat)durationTime {
    _durationTime = durationTime;
    [self change];
}

- (void)change {
    self.replicator.instanceCount = _count;
    // 设置子层动画延长时间
    self.replicator.instanceDelay = _durationTime / _count;
    // 设置子层形变角度
    CGFloat angle = M_PI * 2 / _count;
    self.replicator.instanceTransform = CATransform3DMakeRotation(angle, 0, 0, 1);
}



@end
